package com.zxy.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zxy.annotation.RequestMapping;
import com.zxy.annotation.RequestMappingHandler;
import com.zxy.utils.JSONUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zxy
 * @date 2019-04-28 15:46
 */
@WebServlet(urlPatterns = "/date/*")
public class DateController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        String requestURI = request.getRequestURI();
        String action = requestURI.substring(request.getServletContext().getContextPath().length() + 1);
        action = action.substring(action.indexOf("/"));
        System.out.println(action);
        RequestMappingHandler.handle(this.getClass(), this, action, request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req, resp);
    }

    @RequestMapping("/computeDate")
    public void computeDate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            Gson gson = new Gson();
            Map<String, Object> map = new HashMap<>();
            String json = JSONUtil.getJSONData(request);
            JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
            Date date1=new SimpleDateFormat("yyyy-MM-dd").parse(jsonObject.get("date1").getAsString());
            String diffDay = jsonObject.get("differenceDate").getAsString();
            Date date2 = DateUtils.addDays(date1, Integer.parseInt(diffDay));
            String newDate=new SimpleDateFormat("yyyy-MM-dd").format(date2);
            map.put("date", newDate);
            response.getWriter().write(gson.toJson(map,Map.class));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/differenceDate")
    public void differenceDate(HttpServletRequest request, HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        Gson gson=new Gson();
        String json = JSONUtil.getJSONData(request);
        JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();


        String date2 = jsonObject.get("date2").getAsString();
        String date3 = jsonObject.get("date3").getAsString();
        try {
            Date Date2=new SimpleDateFormat("yyyy-MM-dd").parse(date2);
            Date Date3=new SimpleDateFormat("yyyy-MM-dd").parse(date3);
            String diffDate=(Date2.getTime()-Date3.getTime())/1000/86400+"";
            if (Integer.parseInt(diffDate)<0){
                diffDate=-Integer.parseInt(diffDate)+"";
            }

            jsonObject.addProperty("diffDate",diffDate);
            out.write(gson.toJson(jsonObject));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/javaDateFormat")
    public void javaDateFormat(HttpServletRequest request, HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        String json = JSONUtil.getJSONData(request);
        JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
        String timeStamp=jsonObject.get("javaTimeStamp").getAsString();
        System.out.println(String.format("timeStamp:%s", timeStamp));
        JsonObject responseData = new JsonObject();
        responseData.addProperty("formated", DateFormatUtils.format(new Date(Long.parseLong(timeStamp)),"yyyy-MM-dd HH:mm:ss"));
        out.write(new Gson().toJson(responseData));
    }
}
